<?php
namespace App\Http\Controllers\Business\App\MyCenter;

use App\Models\Collection;
use App\Models\DoubleCoupon;
use App\Models\OrderRefund;
use App\Models\RedEnvelope;
use App\Models\SalesmanInfo;
use App\Models\ShopEvent;
use App\Models\Subscription;
use App\Models\User;
use App\Models\Order;
use App\Models\City;
use App\Models\Invitation;
use App\Models\Feedback;
use App\Models\Recharge;
use App\Models\UnregisteredUser;
use Illuminate\Support\Facades\Redis;
use Log;

class MyCenter
{
    //首页
    public static function index($userid) {
        if (!$userid) {
            return ['status'=>'failed','message'=>'无用户参数！'];
        }
        $data = User::where('id', $userid)->select('nickname','mobile','avatar','paypassword','point')->first();
        if ($data['paypassword']) {
            $data['paypassword'] = '1';
        } else {
            $data['paypassword'] = '0';
        }
        $data['sumdoublecoupons'] = DoubleCoupon::where(array('userid' => $userid, 'status' => '0'))->where('endtime', '>=', date('Y-m-d'))->get()->count();
        $data['sumredenvelopes'] = RedEnvelope::where(array('userid' => $userid, 'status' => '0'))->where('endtime', '>=', date('Y-m-d'))->get()->count();
        return ['status'=>'success','data'=>$data];
    }
    //通过用户id获取用户信息
    public static function getUserInfoByUserId($userId) {
        if (!$userId) {
            return ['status'=>'failed','message'=>'无用户参数！'];
        }
        $double = Recharge::where(array('userid'=>$userId,'doublestatus'=>0))->get();
        foreach ($double as $v) {
            if(strtotime($v['doubletime']) <= time()) {
                $recharge = Recharge::find($v["id"]);
                $recharge->doublestatus = '1';
                $recharge->save();
                User::find($userId)->increment('balance',$v["doublemoney"]);
                User::find($userId)->increment('point',$v["money"]);
            }
        }
        $data = User::where('id', $userId)->select('nickname','mobile','avatar','paypassword','point','realname','identitycard','city', 'sex', 'birthday', 'savemoney', 'doublecoupons', 'balance', 'issalesman', 'created_at')->first();
        if ($data['paypassword']) {
            $data['paypassword'] = '1';
        } else {
            $data['paypassword'] = '0';
        }
        if (isset($data['issalesman']) && ($data['issalesman'] == 1)) {
            $userSalesmanStatus = SalesmanInfo::where('salesmanid', $userId)->pluck('status')->first();
            if(!$userSalesmanStatus) {
                $data['issalesman'] = '0';
            }
            //$data['issalesman'] = '0'; //TODO 关闭个人地推入口
        }
        if(isset($data['realname'])) {
            $data['realname'] = str_replace(mb_substr($data['realname'], 0, 1, 'utf-8'), '*', $data['realname']);
        }
        if(isset($data['identitycard'])) {
            $data['identitycard'] = str_replace(substr($data['identitycard'], 3, 11), '***********', $data['identitycard']);
        }
        if(isset($data['mobile'])) {
            $data['realmobile'] = $data['mobile'];
            $data['mobile'] = str_replace(substr($data['mobile'], 3, 4), '****', $data['mobile']);
        }
        $data['birthday'] = (isset($data['birthday']) && ($data['birthday'] != '')) ?  date('Y-m-d', strtotime($data['birthday'])): '0000-00-00';
        $data['sumdoublecoupons'] = DoubleCoupon::where(array('userid' => $userId, 'status' => '0'))->where('endtime', '>', date('Y-m-d H:i:s', time()))->get()->count();
        $doubleCouponCount = DoubleCoupon::where(array('userid' => $userId, 'type'=>'recharge'))->where('endtime', '>', date('Y-m-d H:i:s', time()))->get()->count();
        if($doubleCouponCount>0) {
            $data['isdoublecoupon'] = '1';
        } else {
            $data['isdoublecoupon'] = '0';
        }
        $data['sumredenvelopes'] = RedEnvelope::where(array('userid' => $userId, 'status' => '0'))->where('endtime', '>=', date('Y-m-d'))->get()->count();
        $data['rechargecount'] = Recharge::where('userid', $userId)->get()->count();
        $data['doublemoney'] = Recharge::where(array('userid'=>$userId,'status'=>'1','doublestatus'=>'0'))->sum('doublemoney');
        if($data['doublemoney'] == '') {
            $data['doublemoney'] = '0.0';
        }
        $data['waitcommentorder'] = Order::where('userid', $userId)->where('commentstatus', '0')->where('refundstatus',0)->get()->count();
        $data['rechargefee'] = 0.06;
        return ['status'=>'success','data'=>$data];
    }
    //翻贝券列表
    public static function doubleCouponsList($userid) {
        if (!$userid) {
            return ['status'=>'failed','message'=>'无用户参数！'];
        }
        $data = DoubleCoupon::where('userid', $userid)->select('id as doublecouponid','endtime','status')->get();
        if ($data) {
            foreach ($data as $k => $v) {
                $result = time() - strtotime($v['endtime']);
                if ($result > 0) {
                    $data[$k]['status'] = '2';
                }
            }
        }
        return ['status'=>'success','data'=>$data];
    }
    //有用的翻贝券列表
    public static function doubleCouponsAvailableList($userId) {
        if(!$userId) {
            return ['status'=>'failed', 'message'=>'无用户参数！'];
        }
        $data = DoubleCoupon::where('userid', $userId)->where('endtime', '>=', date('Y-m-d H:i:s'))->where('status', '0')
            ->select('id as doublecouponid','endtime','status')->paginate(20);
        return ['status'=>'success', 'data'=>$data];
    }
    //不可用的翻贝券列表
    public static function doubleCouponsNotAvailableList($userId) {
        if(!$userId) {
            return ['status'=>'failed', 'message'=>'无用户参数！'];
        }
        $data = DoubleCoupon::where('userid', $userId)->where(function($query) {
                    $query->orwhere('endtime', '<=', date('Y-m-d H:i:s'))
                    ->orwhere('status', '1');
                })->select('id as doublecouponid','endtime','status')->paginate(20);
        if ($data) {
            foreach ($data as $k => $v) {
                $result = time() - strtotime($v['endtime']);
                if ($result > 0) {
                    $data[$k]['status'] = '2';
                }
            }
        }
        return ['status'=>'success', 'data'=>$data];
    }
    //红包列表
    public static function redEnvelopesList($userId) {
        if (!$userId) {
            return ['status'=>'failed','message'=>'无用户参数！'];
        }
        $data = RedEnvelope::where('userid', $userId)
            ->orderBy('created_at', 'desc')
            ->select('id as redenvelopeid','amount','minimumamount','shopid','endtime','usetime','status', 'cityid','firstclassid','secondclassid', 'firstclassname', 'secondclassname')
            ->get();
        if ($data) {
            foreach ($data as $k => $v) {
                if($data[$k]['status'] != '1') {
                    //未使用的红包过期状态变成2
                    $result = time() - strtotime($v['endtime']);
                    if ($result > 0) {
                        $data[$k]['status'] = '2';
                    }
                }
                $data[$k]['cityname'] = City::where('id', $data[$k]['cityid'])->pluck('name')->first();
            }
        }
        return ['status'=>'success','data'=>$data];
    }
    //可用的红包列表
    public static function redEnvelopesAvailableList($userId) {
        if(!$userId) {
            return ['status'=>'failed', 'message'=>'无用户参数！'];
        }
        $data = RedEnvelope::where('userid', $userId)
            ->where('endtime', '>=', date('Y-m-d'))
            ->where('status', '0')
            ->orderBy('created_at', 'desc')
            ->select('id as redenvelopeid','amount','minimumamount','shopid','endtime','usetime','status', 'cityid','firstclassid','secondclassid', 'firstclassname', 'secondclassname')
            ->paginate(20);
        foreach ($data as $k => $v) {
            $data[$k]['cityname'] = City::where('id', $data[$k]['cityid'])->pluck('name')->first();
        }
        return ['status'=>'success', 'data'=>$data];
    }
    //不可用的红包列表
    public static function redEnvelopesNotAvailableList($userId) {
        if(!$userId) {
            return ['status'=>'failed', 'message'=>'无用户参数！'];
        }
        $data = RedEnvelope::where('red_envelopes.userid', $userId)
            ->join('city', 'red_envelopes.cityid', '=', 'city.id')
            ->where(function($query) {
                $query->orwhere('red_envelopes.endtime', '<', date('Y-m-d'))
                    ->orwhere('red_envelopes.status', 3)
                    ->orwhere('red_envelopes.status', 1)
                    ->orwhere('red_envelopes.status', 2);
            })
            ->orderBy('red_envelopes.updated_at', 'desc')
            ->select('red_envelopes.id as redenvelopeid','red_envelopes.amount','red_envelopes.minimumamount','red_envelopes.shopid', 'red_envelopes.endtime','red_envelopes.usetime','red_envelopes.status',
                'city.id as cityid','city.name as cityname','red_envelopes.firstclassid','red_envelopes.secondclassid', 'red_envelopes.firstclassname', 'red_envelopes.secondclassname', 'red_envelopes.updated_at')
            ->paginate(20);
        foreach ($data as $k => $v) {
            $data[$k]['refundtime'] = '0000-00-00 00:00:00';
            if($data[$k]['status'] == '0') {
                //未使用的红包过期状态变成2
                $result = time() - strtotime($v['endtime']);
                if ($result > 0) {
                    $data[$k]['status'] = 2;
                }
            } elseif($data[$k]['status'] == 3) {
                $data[$k]['refundtime'] = date('Y-m-d H:i:s', strtotime($data[$k]['updated_at']));
            }
            $data[$k]['cityname'] = City::where('id', $data[$k]['cityid'])->pluck('name')->first();
        }
        return ['status'=>'success', 'data'=>$data];
    }
    //获取用户详情
    public static function getUserInfo($userid) {
        if (!$userid) {
            return ['status'=>'failed','message'=>'无用户参数！'];
        }
        $data = User::where('id', $userid)->select('mobile','nickname','avatar','sex','birthday','city','realname','identitycard')->first();
        if(!$data['realname'] || !$data['identitycard']) {
            $data['realname'] = '';
            $data['identitycard'] = '';
        } else {
            $data['realname'] = str_replace(mb_substr($data['realname'],0, 1, 'utf-8'),'*',$data['realname']);
            $data['identitycard'] = str_replace(substr($data['identitycard'],3,11),'***********',$data['identitycard']);
        }
        $data['birthday'] = (isset($data['birthday']) && ($data['birthday']!='')&&($data['birthday']!= '0000-00-00 00:00:00'))? date('Y-m-d', strtotime($data['birthday'])):"0000-00-00";
        return ['status'=>'success','data'=>$data];
    }
    //编辑用户信息
    public static function saveUserInfo($userid,$nickname,$sex='',$birthday='',$city='') {
        if (!$userid || !$nickname) {
            return ['status'=>'failed','message'=>'无用户参数！'];
        }
        $data = User::find($userid);
        $data->sex = $sex;
        $data->birthday = $birthday;
        $data->city = $city;
        $data->nickname = $nickname;
        $data->save();
        return ['status'=>'success','message'=>'保存成功！'];
    }
    //保存用户信息
    public static function saveUserAvatar($userid, $avatar) {
        if(!$userid || !$avatar) {
            return ['status'=>'failed','message'=>'参数不对！'];
        }
        $data = User::where('id', $userid)->first();
        $data->avatar = $avatar;
        $data->save();
        return ['status'=>'success','message'=>'保存成功！'];
    }
    //获取身份信息
    public static function getIdentity($userId) {
        if (!$userId) {
            return ['status'=>'failed','message'=>'参数不对！'];
        }
        $data = User::where('id', $userId)->select('mobile', 'realname', 'identitycard')->first();
        if(isset($data['mobile'])) {
            $data['mobile'] = str_replace(substr($data['mobile'], 3, 4), '****', $data['mobile']);
        }
        if(isset($data['realname'])) {
            $data['realname'] = str_replace(mb_substr($data['realname'], 0, 1, 'utf-8'), '*', $data['realname']);
        }
        if(isset($data['identitycard'])) {
            $data['identitycard'] = str_replace(substr($data['identitycard'], 3, 11), '***********', $data['identitycard']);
        }
        return ['status'=>'success', 'data'=>$data];
    }
    //保存身份信息
    public static function saveIdentity($userid, $realname, $identitycard) {
        if (!$userid || !$realname || !$identitycard) {
            return ['status'=>'failed','message'=>'请输入正确格式！'];
        }
        if(self::isCreditNo($identitycard) == false) {
            return ['status'=>'failed','message'=>'身份证格式不对！'];
        }
        $data = User::find($userid);
        $data->realname = $realname;
        $data->identitycard = $identitycard;
        $data->save();
        return ['status'=>'success','message'=>'保存成功！'];
    }
    //检测用户信息是否正确
    public static function verifyIdentity($userid, $realname, $identitycard) {
        if (!$userid || !$realname || !$identitycard) {
            return ['status'=>'failed','message'=>'请输入正确格式！'];
        }
        $data = User::find($userid);
        if(($data->realname != $realname) || ($data->identitycard != $identitycard)) {
            return ['status' => 'failed', 'message' => '认证信息不匹配！'];
        } else {
            return ['status' => 'success', 'message' => '认证信息匹配！'];
        }
    }
    //更换手机信息
    public static function updateMobile($userid, $oldmobile, $newmobile, $code) {
        if (!$userid || !$oldmobile || !$newmobile || !$code) {
            return ['status'=>'failed','message'=>'请输入正确格式！'];
        }
        if($code != Redis::get('User:SmsMobile:'.$newmobile)) {
            return ['status'=>'failed','message'=>'验证码错误！'];
        }
        $data = User::find($userid);
        if($data->mobile != $oldmobile) {
            return ['status'=>'failed','message'=>'手机号码不对！'];
        }
        if (!is_numeric($newmobile) || (strlen($newmobile) != 11)) {
            return ['status' => 'failed', 'message' => '手机号码格式不对！'];
        }
        $count = User::where('mobile', $newmobile)->get()->count();
        if($count > 0) {
            return ['status'=>'failed','message'=> $newmobile.'已绑定其他账号！'];
        } else {
            $data->mobile = $newmobile;
            $data->save();
            return ['status' => 'success', 'message' => '保存成功！'];
        }
    }
    //收藏列表
    public static function collectionList($userid, $page=1, $length=20){
        if (!$userid) {
            return ['status'=>'failed','message'=>'无用户参数！'];
        }
        $skip = ($page - 1) * $length;
        $data = Collection::where('collections.userid',$userid)->join('shops', 'collections.shopid', '=', 'shops.id')
            ->select('collections.id as collectionid','collections.shopid','shops.logo','shops.name','shops.secondclassname','shops.districtname','shops.businessareaname','shops.label','shops.address','shops.todayevent','shops.nextevent','shops.status as shopstatus')
            ->orderBy('collections.updated_at', 'desc')
            ->skip($skip)->take($length)
            ->get();
        return ['status'=>'success','data'=>$data];
    }
    //获取收藏状态
    public static function getCollectionStatus($userid, $shopid) {
        if(!$userid || !$shopid) {
            return ['status'=>'failed','message'=>'无用户参数！'];
        }
        $count = Collection::where('userid', $userid)->where('shopid', $shopid)->get()->count();
        if($count > 0) {
            return ['status'=>'success', 'message'=>'已添加'];
        } else {
            return ['status'=>'failed','message'=>'未添加！'];
        }
    }
    //删除收藏
    public static function delCollection($userid,$collectionid) {
        if (!$userid || !$collectionid) {
            return ['status'=>'failed','message'=>'无用户参数！'];
        }
        Collection::where(array('userid'=>$userid,'id'=>$collectionid))->delete();
        return ['status'=>'success','message'=>'删除成功！'];
    }
    //通过商家ID 删除收藏
    public static function delCollectionByShopId($userid, $shopid) {
        if (!$userid || !$shopid) {
            return ['status'=>'failed','message'=>'收藏失败！'];
        }
        Collection::where(array('userid'=>$userid,'shopid'=>$shopid))->delete();
        return ['status'=>'success','message'=>'取消收藏！'];
    }
    //新增收藏
    public static function addCollection($userid,$shopid) {
        if (!$userid || !$shopid) {
            return ['status'=>'failed','message'=>'收藏失败'];
        }
        $collection = new Collection;
        $collection->userid=$userid;
        $collection->shopid=$shopid;
        $collection->save();
        return ['status'=>'success','message'=>'收藏成功！'];
    }
    //是否收藏
    public static function isCollection($userid, $shopid) {
        $count = Collection::where('userid',$userid)->where('shopid', $shopid)->get()->count();
        if($count > 0) {
            return ['status'=>'success'];
        } else {
            return ['status'=>'failed'];
        }
    }
    //订阅列表
    public static function subscriptionList($userid, $page=1, $length=20) {
        if (!$userid) {
            return ['status'=>'failed','message'=>'无用户参数！'];
        }
        $skip = ($page - 1) * $length;
        $data = Subscription::where('subscriptions.userid',$userid)->join('shops', 'subscriptions.shopid', '=', 'shops.id')
            ->select('subscriptions.id as subscriptionid','subscriptions.shopid','subscriptions.event','shops.logo','shops.name','shops.secondclassname','shops.districtname','shops.businessareaname','shops.label','shops.address','shops.todayevent','shops.nextevent','shops.status as shopstatus')
            ->orderBy('subscriptions.updated_at', 'desc')
            ->skip($skip)->take($length)
            ->get();
        return ['status'=>'success','data'=>$data];
    }
    //订阅状态
    public static function getSubscriptionStatus($userid, $shopid) {
        if(!$userid || !$shopid) {
            return ['status'=>'failed','message'=>'无用户参数！'];
        }
        $count = Subscription::where('userid', $userid)->where('shopid', $shopid)->get()->count();
        if($count > 0) {
            return ['status'=>'success', 'message'=>'已添加'];
        } else {
            return ['status'=>'failed','message'=>'未添加！'];
        }
    }
    //删除订阅
    public static function delSubscription($userid,$subscriptionid) {
        if (!$userid || !$subscriptionid) {
            return ['status'=>'failed','message'=>'删除失败'];
        }
        Subscription::where(array('userid'=>$userid,'id'=>$subscriptionid))->delete();
        return ['status'=>'success','message'=>'删除成功！'];
    }
    //通过商家ID删除订阅
    public static function delSubscriptionByShopid($userid,$shopid){
        if (!$userid || !$shopid) {
            return ['status'=>'failed','message'=>'删除失败'];
        }
        Subscription::where(array('userid'=>$userid,'shopid'=>$shopid))->delete();
        return ['status'=>'success','message'=>'删除成功！'];
    }
    //新增订阅
    public static function addSubscription($userid,$shopid) {
        if (!$userid || !$shopid) {
            return ['status'=>'failed','message'=>'订阅失败'];
        }
        $shopEvent = ShopEvent::where('shopid',$shopid)->pluck('event')->first();
        $subscriptionEvent = Subscription::where('shopid',$shopid)->where('userid',$userid)->where('event',$shopEvent)->count();
        if($subscriptionEvent >0){
            return ['status'=>'failed','message'=>'您已订阅本店'];
        }else{
            $subscription = new Subscription;
            $subscription->userid = $userid;
            $subscription->shopid = $shopid;
            $subscription->event = $shopEvent;
            $subscription->save();
            return ['status'=>'success','message'=>'订阅成功！'];
        }
    }
    //是否订阅
    public static function isSubscription($userid, $shopid) {
        $count = Subscription::where('userid',$userid)->where('shopid', $shopid)->get()->count();
        if($count > 0) {
            return ['status'=>'success'];
        } else {
            return ['status'=>'failed'];
        }
    }
    //验证支付密码
    public static function validataPayPassword($userid,$paypassword) {
        if (!$userid || strlen($paypassword) != 6 || is_numeric($paypassword)==false) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        $user = User::find($userid);
        if($user) {
            if($user['paypassword'] == md5($paypassword.env('PASSWORD_SALT'))) {
                return ['status'=>'success', 'message'=>'验证成功'];
            } else {
                return ['status'=>'failed','message'=>'密码错误！'];
            }
        } else {
            return ['status'=>'failed','message'=>'用户不存在！'];
        }
    }
    //保存支付密码
    public static function setPayPassword($userid,$paypassword) {
        if (!$userid || strlen($paypassword) != 6 || is_numeric($paypassword)==false) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        $user = User::find($userid);
        if($user) {
            if($user['paypassword']) {
                return ['status'=>'failed','message'=>'用户已存在支付密码！'];
            } else {
                $user->paypassword = md5($paypassword.env('PASSWORD_SALT'));
                $user->save();
                return ['status'=>'success','message'=>'支付密码设置成功！'];
            }
        } else {
            return ['status'=>'failed','message'=>'用户不存在！'];
        }
    }
    //删除支付密码
    public static function delPayPassword($userid,$paypassword) {
        if (!$userid || strlen($paypassword) != 6 || is_numeric($paypassword)==false) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        $user = User::find($userid);
        if($user) {
            if($user['paypassword'] == md5($paypassword.env('PASSWORD_SALT'))) {
                $user->paypassword = '';
                $user->save();
                return ['status'=>'success','message'=>'支付密码关闭成功！'];
            } else {
                return ['status'=>'failed','message'=>'密码错误！'];
            }
        } else {
            return ['status'=>'failed','message'=>'用户不存在！'];
        }
    }

    public static function verifyPayPassword($userid, $paypassword) {
        if((!$userid) || strlen($paypassword) != 6 ) {
            return ['status' => 'failed', 'message' => '参数错误！'];
        }
        $user = User::find($userid);
        if($user) {
            if ($user['paypassword'] == md5($paypassword . env('PASSWORD_SALT'))) {
                return ['status'=>'success','message'=>'支付密码正确！'];
            } else {
                return ['status'=>'failed','message'=>'密码错误！'];
            }
        } else {
            return ['status'=>'failed','message'=>'用户不存在！'];
        }
    }
    //更新支付密码
    public static function updatePayPasswordForOldPayPassword($userid,$oldpaypassword,$newpaypassword) {
        if (!$userid || strlen($oldpaypassword) != 6 || is_numeric($oldpaypassword)==false || strlen($newpaypassword) != 6 || is_numeric($newpaypassword)==false) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        $user = User::find($userid);
        if($user) {
            if($user['paypassword'] == md5($oldpaypassword.env('PASSWORD_SALT'))) {
                $user->paypassword = md5($newpaypassword.env('PASSWORD_SALT'));
                $user->save();
                return ['status'=>'success','message'=>'支付密码更新成功！'];
            } else {
                return ['status'=>'failed','message'=>'密码错误！'];
            }
        } else {
            return ['status'=>'failed','message'=>'用户不存在！'];
        }
    }

    public static function updatePayPasswordForValidataIdentity($userid,$realname,$identitycard,$newpaypassword) {
        if (!$userid || strlen($newpaypassword) != 6 || is_numeric($newpaypassword)==false || !$realname || !$identitycard) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        $user = User::find($userid);
        if($user) {
            if($user['realname'] == $realname && $user['identitycard'] == $identitycard) {
                $user->paypassword = md5($newpaypassword.env('PASSWORD_SALT'));
                $user->save();
                return ['status'=>'success','message'=>'支付密码更新成功！'];
            } else {
                return ['status'=>'failed','message'=>'实名信息错误！'];
            }
        } else {
            return ['status'=>'failed','message'=>'用户不存在！'];
        }
    }

    //保存认证信息
    public static function validataIdentity($userid,$realname,$identitycard) {
        if (!$userid || !$realname || !$identitycard) {
            return ['status'=>'failed','message'=>'无用户参数！'];
        }
        if(self::isCreditNo($identitycard)==true) {
            $user = User::find($userid);
            $user->realname = $realname;
            $user->identitycard = $identitycard;
            $user->save();
            return ['status'=>'success','message'=>'实名认证成功！'];
        } else {
            return ['status'=>'failed','message'=>'身份证号码不合法！'];
        }
    }

    public static function recommend($parentId, $mobile, $type='doublecoupon') {
        if(!$parentId || !$mobile || !$type) {
            return ['status'=>'failed', 'message'=>'领取失败'];
        }
        $count = User::where('mobile', $mobile)->get()->count();
        if($count > 0) {
            return ['status'=>'failed', 'message'=>'您已是注册用户，不能参加此活动'];
        }
        $unregisteredUserCount = UnregisteredUser::where('mobile', $mobile)->where('parentid', $parentId)->get()->count();
        if($unregisteredUserCount > 0) {
            return ['status'=>'failed', 'message'=>'您已领取过了'];
        }
        $unregisteredUser = UnregisteredUser::create([
            'mobile' => $mobile,
            'parentid' => $parentId,
            'type' => $type,
        ]);

        return ['status'=>'success', 'message'=>'领取成功'];
    }

    public static function addFeedback($userid, $content, $username='', $mobile='') {
        if(!$userid || !$content) {
            return ['status'=>'failed', 'message'=>'参数错误或者内容不能为空'];
        }
        $feedback = FeedBack::create([
            'userid' => $userid,
            'username' => $username,
            'mobile' => $mobile,
            'content' => $content
        ]);
        $feedback->save();
        return ['status'=>'success', 'message'=>'添加意见反馈成功'];
    }

    public static function isCreditNo($vStr) {
        $vCity = array(
            '11','12','13','14','15','21','22',
            '23','31','32','33','34','35','36',
            '37','41','42','43','44','45','46',
            '50','51','52','53','54','61','62',
            '63','64','65','71','81','82','91'
        );

        if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr)) return false;

        if (!in_array(substr($vStr, 0, 2), $vCity)) return false;

        $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
        $vLength = strlen($vStr);

        if ($vLength == 18) {
            $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
        } else {
            $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
        }

        if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) return false;
        if ($vLength == 18) {
            $vSum = 0;

            for ($i = 17 ; $i >= 0 ; $i--) {
                $vSubStr = substr($vStr, 17 - $i, 1);
                $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr , 11));
            }

            if($vSum % 11 != 1) return false;
        }

        return true;
    }
}